package com.cskaoyan.service;

import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.bean.MarketSearchHistory;
import com.cskaoyan.bean.MarketSearchHistoryExample;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class AdminSearchHistoryServiceImpl implements AdminSearchHistoryService {
    @Override
    public CommonData<MarketCoupon> list(Integer userId, String keyword, Integer page, Integer limit, String sort, String order) {
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();

        MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();

        if (userId != null) {

            criteria.andIdEqualTo(userId);

        }
        if (!StringUtil.isEmpty(keyword)) {

            criteria.andKeywordLike("%" + keyword + "%");


        }

        if (!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)) {
            marketSearchHistoryExample.setOrderByClause(sort + " " + order);
        }

        SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();

        SqlSession sqlSession = sqlSessionFactory.openSession();

        PageHelper.startPage(page, limit);

        MarketSearchHistoryMapper mapper = sqlSession.getMapper(MarketSearchHistoryMapper.class);

        List <MarketSearchHistory> marketSearchHistories = mapper.selectByExample(marketSearchHistoryExample);


        if (sqlSession != null) {

            sqlSession.close();

        }

      return   CommonData.data(new PageInfo(marketSearchHistories));




   }
}
